<?php 
//include 'Model.class.php';
class userModel extends Model
{
	private $tb='usr';
	private $tb2='usr_detail';

	public function add($data)
	{
		if($this->db->insert('usr',$data))
		{
			return $this->db->insert_id();
		}
		return false;
	}
	//修改某个字段值
	public function modify($uid,$attr,$val)
	{
		$sql="update $this->tb set $attr='$val' where id=$uid";
		if($this->db->set_query($sql))
		{
			return $this->db->affect_row();
		}
		return false;
	}
	//获取单个字段值
	public function getAttr($uid,$attr)
	{
		$sql="select $attr from $this->tb where id=$uid";
		return $this->db->getOne($sql);
	}
	
	//通过账号查找密码
	public function findpwd($acc)
	{
		$sql="select pwd from usr where account='$acc'";
		$rs=$this->db->getOne($sql);
		if($rs)
		{
			return $rs;
		}
		else
		{
			return false;
		}
	}
	
	//获取最大的某个值
	public function getMax($type)
	{
		$sql="select max($type) from $this->tb";
		return $this->db->getOne($sql);
	}

	//通过账号查找用户
	public function find($acc)
	{
		$sql="select * from usr where account='$acc'";
		$rs=$this->db->getRow($sql);
		if($rs)
		{
			return $rs;
		}
		else
		{
			return false;
		}
	}
	//通过邮箱查找用户
	public function findEmail($email)
	{
		$sql="select * from usr where email='$email'";
		$rs=$this->db->getRow($sql);
		if($rs)
		{
			return $rs;
		}
		else
		{
			return false;
		}
	}
	/*
	获取消息所需的信息
	*/
	public function getbycom($uid)
	{
		$sql="select name,isvip,account,img32,img16 from $this->tb where id=$uid";
		return $this->db->getRow($sql);
	}
	public function getbyid($uid)
	{
		$sql="select * from $this->tb where id=$uid";
		return $this->db->getRow($sql);
	}

	public function getname($uid)
	{
		$sql="select name from $this->tb where id=$uid";
		return $this->db->getOne($sql);
	}
	public function findbyid($uid)
	{
		$sql="select * from $this->tb where id=$uid";
		return $this->db->getRow($sql);
	
	}

	//根据条件判断是否有用户
	public function hasuser($attr,$val)
	{
		$sql="select count(*) from $this->tb where $attr='$val'";
		$count=$this->db->getOne($sql);
		if($count>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	
	//判断用户是否vip会员
	public function isvip($uid)
	{
		$sql="select isvip from $this->tb where id=$uid";
		if($this->db->getOne($sql))
		{
			return true;
		}
		return false;
	}



	/*
		用户在线状态更改
		0 离线
		1 在线
		2 Q我
		3 离开
		4 忙碌
		5 勿扰
		6 隐身
		状态每35秒即销毁，用户每30秒发送更新好友状态请求时
		都会先更新自身状态
		离线状态设为0，即永不过期
	*/
	public function changeStatus($id,$statuscode,$t=0)
	{
		//if($this->getStatus($id))
		//{
		//	return $this->mem->replace($id,$statuscode,$t);
		//}
		//else
		//{
			//如果设置为离线则直接删除
			if(intval($statuscode)==0)
			{
				return $this->mem->del($id);
			}
			return $this->mem->set($id,$statuscode,$t);
		//}
	}

	//用户在线状态获取
	public function getStatus($id)
	{
		return $this->mem->get($id);
	}
	//批量获取用户在线状态
	public function getAllstatus(&$arr)
	{
		//$starr=array();
		for($i=0;$i<count($arr);$i++)
		{
			$s=$this->mem->get($arr[$i]['frdid']);
			$arr[$i]['status']=$s==false?'0':$s;
		}
		return $arr;
	}

	//批量获取用户在线状态
	public function getAllstatusbyid($arr)
	{
		$status_arr=array();
		for($i=0;$i<count($arr);$i++)
		{
			$a=array();
			$a['fid']=$arr[$i];
			$s=$this->mem->get($arr[$i]);
			$a['status']=$s==false?'0':$s;
			//$a['status']=$a['status']=='6'?'0':$a['status'];
			$status_arr[]=$a;
		}
		return $status_arr;
	}

	//将用户正在输入状态加入memecache
	public function write($uid,$toid,$t=3)
	{
		$toid="to".$toid;
		return $this->mem->set($toid,$uid,$t);
	}

	//取出指定用户的单个授予输入状态
	public function getWrite($uid)
	{
		$uid="to".$uid;
		return $this->mem->get($uid);
	}


	//将session加入mem
	public function addSeon($id,$session)
	{
		$sessid='sess'.$id;
		return $this->mem->set($sessid,$session);
	}
	//获取mem中的session
	public function getSeon($id)
	{
		$sessid='sess'.$id;
		return $this->mem->get($sessid);
	}
	//删除mem中的session
	public function delSeon($id)
	{
		$sessid='sess'.$id;
		return $this->mem->del($sessid);
	}



	//通过账号查找
	//返回单条条目
	public function getbyacc($acc)
	{
		$sql="select * from $this->tb where account='$acc'";
		return $this->db->getRow($sql);
	}
	//通过昵称关键字查找
	//暂时返回20个条目
	public function  getbyname($name)
	{
		$sql="select * from $this->tb where name like '%".$name."%' limit 20";
		return $this->db->getAll($sql);
	}
	
	/*
		更新登录时间
	*/
	public function setlogin($uid)
	{
		$sql="update $this->tb set lastlogin=".time()." where id=$uid";
		if($this->db->set_query($sql))
		{
			return $this->db->affect_row();
		}
		else
		{
			return false;
		}
	}
	/*
		更新下线时间
	*/
	public function setlogout($uid,$time)
	{
		$sql="update $this->tb set lastlogout=$time where id=$uid";
		if($this->db->set_query($sql))
		{
			return $this->db->affect_row();
		}
		else
		{
			return false;
		}
	}

	//通过账号查找用户指定大小的头像
	public function getimg($acc,$size=100)
	{
		$sql="select imghead_".$size." from $this->tb where account=".$acc;
		return $this->db->getOne($sql);
	}



	//get as friends info
	public function getinfo($uid)
	{
		$sql="select name,isvip,account,imghead_60 as img,imghead_30 as img30,imghead_100 as img100,imghead_gray as imggray,lv,sign,info,livetown,hometown,birthday,sex,email,introcard_skin from usr where id=".$uid;
		return $this->db->getRow($sql);
	}

	//获取陌生人模式
	public function getStranger($uid)
	{
		$sql="select id,name,isvip,account,imghead_60 as img,imghead_30 as img30,imghead_100 as img100,imghead_gray as imggray,lv,info,livetown,hometown,birthday from usr where id=".$uid;
		return $this->db->getRow($sql);
	}

	//根据需要获取信息
	public function getsome($proprety_value,$data,$proprety='id',$tb='')
	{
		$tb==''?$tb=$this->tb:'';
		$sql="select ".implode(',', array_values($data))." from $tb where $proprety=$proprety_value";
		//var_dump($sql);

		return $this->db->getRow($sql);
	}

	//根据需要设置信息
	public function setsome($uid,$data,$tb='')
	{
		$tb==''?$tb=$this->tb:'';
		return  $this->db->update($tb,$data,$uid);
	}

	//根据需要获取单个信息
	public function getone($value,$attr,$proprety='id',$tb='')
	{
		$tb==''?$tb=$this->tb:'';
		$sql="select $attr from $tb where $proprety=$value";

		return $this->db->getOne($sql);
	}
	//根据需要设置单个信息
	public function setone($proprety_value,$attr,$val,$proprety='id',$tb='')
	{
		$tb==''?$tb=$this->tb:'';
		$sql="update $tb set $attr='$val' where $proprety=$proprety_value";

		return $this->db->set_query($sql);
	}
	

	//获取详细
	public function getdetail($uid)
	{
		$sql="select * from $this->tb2 where ownid=$uid";
		return $this->db->getRow($sql);
	}
	//修改详细
	/*
	参数 
	array(
		设置名=>设置值
	);
	将数组内所有设置名=>设置值
	*/
	public function setdetail($arr,$uid)
	{
		
		return $this->db->updatebyattr($this->tb2,$arr,'ownid',$uid);
	}

	public function setbasic($arr,$uid)
	{
		
		return $this->db->updatebyattr($this->tb,$arr,'id',$uid);
	}




	
	/*通过自定义条件查找未添加为好友的用户
	
		offset 页数
	*/
	public function getfrdbycondition($uid,$conditionarr,$offsetid=0)
	{
		$sql='select id,name,account,imghead_30,hometown,livetown,birthday,sex from '.$this->tb.' where id not in(select frdid from frd where ownid='.$uid.') and id!='.$uid;
		//$sql='select id,name,account,img,hometown,livetown,birthday,sex from'.$this->tb.'where id not in(select frdid from frd where ownid='.$uid.') and id!='.$uid;
		foreach($conditionarr as $k=>$v)
		{
			
			if($k=='keyword')
				continue;
			$sql.=' and '.$k.'='.$v;
		}

		$sql.=' and id>'.$offsetid.' limit 20';
		
		//var_dump($sql);
		return $this->db->getAll($sql);
	}




	public function getMem()
	{
		/*
		$a=array();
		//$a['stats']=$this->mem->getStats();
		//$a['ExtendedStats']=$this->mem->getExtendedStats();
		//$a['keys']=$this->mem->getAllKeys();
		//$a['version']=$this->mem->getVer();
		//return $a;
		//return $this->mem->getStats('items');



		$host='localhost';
		$port=11211;

		$items=$this->mem->getExtendedStats('items');
        $items=$items["$host:$port"]['items'];
        foreach($items as $key=>$values)
		{
			$number=$key;
			$str=$this->mem->getExtendedStats("cachedump",$number,0);
			$line=$str["$host:$port"];
			if(is_array($line) && count($line)>0)
			{
				foreach($line as $key=>$value)
				{
				   //echo $key.'=>';
				   // print_r($this->mem->get($key));
				   //echo "\r\n";
				   $a[]=$this->mem->get($key);
				}
			}
		
		}
		return $a;
		*/
	}

}